package com.ruoyi.cpsp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cpsp.domain.*;
import com.ruoyi.cpsp.dto.ApprovalRecordModifyDto;
import com.ruoyi.cpsp.mapper.ApprovalRecordMapper;
import com.ruoyi.cpsp.mapper.EvaluateRecordDetailMapper;
import com.ruoyi.cpsp.mapper.RegulationDetailMapper;
import com.ruoyi.cpsp.mapper.RegulationTypeMapper;
import com.ruoyi.cpsp.service.ApprovalRecordService;
import com.ruoyi.cpsp.service.EvaluateRecordDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApprovalRecordServiceImpl implements ApprovalRecordService {

    @Autowired
    ApprovalRecordMapper approvalRecordMapper;

    @Autowired
    EvaluateRecordDetailMapper evaluateRecordDetailMapper;

    @Autowired
    RegulationTypeMapper regulationTypeMapper;

    @Autowired
    RegulationDetailMapper regulationDetailMapper;

    @Autowired
    private EvaluateRecordDetailService detailService;

    @Override
    public int insert(ApprovalRecordModifyDto record) {

        Date date = new Date();
        System.out.println(date);

        ApprovalRecord approvalRecord = new ApprovalRecord();
        approvalRecord.setApplyPersonId(record.getApplyPersonId());
        approvalRecord.setApplyPersonName(record.getApplyPersonName());
        approvalRecord.setDetailId(record.getDetailId());
        approvalRecord.setApplyPersonDeptId(record.getApplyPersonDeptId());
        approvalRecord.setApplyPersonDept(record.getApplyPersonDept());
        approvalRecord.setViolationPersonName(record.getViolationPersonName());
        approvalRecord.setOperateType(record.getOperateType());
        approvalRecord.setEmployeeIdCard(record.getEmployeeIdCard());
        approvalRecord.setRegulationTypeId(record.getRegulationTypeId());
        approvalRecord.setRegulationDetailId(record.getRegulationDetailId());
        approvalRecord.setState(record.getState());
        approvalRecord.setCreateTime(date);
        return approvalRecordMapper.insert(approvalRecord);
    }

    @Override
    public ApprovalRecord getone(int id) {
        return approvalRecordMapper.selectByPrimaryKey(id);
    }

    //更新申请列表内容
    @Override
    public int update(ApprovalRecord record) {
        return approvalRecordMapper.updateById(record);
    }

    //同意修改或删除申请
    @Override
    public int agree(int id) {
        ApprovalRecord record = approvalRecordMapper.selectByPrimaryKey(id);
        record.setState(1);
        return approvalRecordMapper.updateById(record);
    }

    //拒绝修改或删除申请
    @Override
    public int reject(int id) {
        ApprovalRecord record = approvalRecordMapper.selectByPrimaryKey(id);
        record.setState(2);
        int detailId = record.getDetailId();

        EvaluateRecordDetail evaluateRecordDetail = detailService.selectByPrimaryKey(detailId);
        evaluateRecordDetail.setState(1);
        detailService.updateByIdSelective(evaluateRecordDetail);

        return approvalRecordMapper.updateById(record);
    }

    //返回待审批列表
    @Override
    public List<ApprovalRecord> select() {
        int state = 0;
        List<ApprovalRecord> list = approvalRecordMapper.selectAll(state);
        return list;
    }

    //查询修改或删除前后内容详情
    @Override
    public ApprovalRecordOldNew detail(int id) {
        //获取修改或删除前的相关数据
        ApprovalRecordOldNew approvalRecordOldNew = new ApprovalRecordOldNew();
        ApprovalRecord record = approvalRecordMapper.selectByPrimaryKey(id);
        int detailId = record.getDetailId();

        EvaluateRecordDetail evaluateRecordDetail = detailService.selectByPrimaryKey(detailId);
        String employeeIdCardOld = evaluateRecordDetail.getEmployeeIdCard();

        int regulationTypeIdOld = evaluateRecordDetail.getRegulationTypeId();
        int regulationDetailIdOld = evaluateRecordDetail.getRegulationDetailId();

        RegulationDetail regulationDetailOld = regulationDetailMapper.selectByPrimaryKey(regulationDetailIdOld);
        String regulationDetailDescribeOld = regulationDetailOld.getDescribe();

        RegulationType regulationTypeOld = regulationTypeMapper.selectByPrimaryKey(regulationTypeIdOld);
        String regulationTypeNameOld = regulationTypeOld.getName();

        approvalRecordOldNew.setEmployeeIdCardOld(employeeIdCardOld);
        approvalRecordOldNew.setRegulationTypeNameOld(regulationTypeNameOld);
        approvalRecordOldNew.setRegulationDetailDescribeOld(regulationDetailDescribeOld);

        //获取修改或删除后的相关数据
        if(record.getOperateType() != 0){
            //修改
            String employeeIdCardNew = record.getEmployeeIdCard();

            int regulationTypeIdNew = record.getRegulationTypeId();
            RegulationType regulationTypeNew = regulationTypeMapper.selectByPrimaryKey(regulationTypeIdNew);
            String regulationTypeNameNew = regulationTypeNew.getName();

            int regulationDetailIdNew = record.getRegulationDetailId();
            RegulationDetail regulationDetailNew = regulationDetailMapper.selectByPrimaryKey(regulationDetailIdNew);
            String regulationDetailDescribeNew = regulationDetailNew.getDescribe();

            approvalRecordOldNew.setEmployeeIdCardNew(employeeIdCardNew);
            approvalRecordOldNew.setRegulationTypeNameNew(regulationTypeNameNew);
            approvalRecordOldNew.setRegulationDetailDescribeNew(regulationDetailDescribeNew);

        }else{
            //删除
            String employeeIdCardNew = "申请删除记录";
            String regulationTypeNameNew = "申请删除记录";
            String regulationDetailDescribeNew = "申请删除记录";

            approvalRecordOldNew.setEmployeeIdCardNew(employeeIdCardNew);
            approvalRecordOldNew.setRegulationTypeNameNew(regulationTypeNameNew);
            approvalRecordOldNew.setRegulationDetailDescribeNew(regulationDetailDescribeNew);

        }

        return approvalRecordOldNew;

    }


}
